import request from '@/utils/request'

// 查询设备列表
export function listDevice(query) {
  return request({
    url: '/energy/device/list',
    method: 'get',
    params: query
  })
}

// 查询设备详细
export function getDevice(deviceId) {
  return request({
    url: '/energy/device/' + deviceId,
    method: 'get'
  })
}

// 新增设备
export function addDevice(data) {
  return request({
    url: '/energy/device',
    method: 'post',
    data: data
  })
}

// 修改设备
export function updateDevice(data) {
  return request({
    url: '/energy/device',
    method: 'put',
    data: data
  })
}

// 删除设备
export function delDevice(deviceId) {
  return request({
    url: '/energy/device/' + deviceId,
    method: 'delete'
  })
}

// 导出设备
export function exportDevice(query) {
  return request({
    url: '/energy/device/export',
    method: 'post',
    params: query
  })
}

// 查询设备下拉树结构
export function treeselect() {
  return request({
    url: '/energy/device/treeselect',
    method: 'get'
  })
}

// 根据部门查询设备列表
export function listDeviceByDept(deptId) {
  return request({
    url: '/energy/device/listByDept/' + deptId,
    method: 'get'
  })
}

// 校验设备编码
export function checkDeviceCodeUnique(data) {
  return request({
    url: '/energy/device/checkDeviceCodeUnique',
    method: 'post',
    data: data
  })
}

// 获取设备导入模板
export function importTemplate() {
  return request({
    url: '/energy/device/importTemplate',
    method: 'post'
  })
}

// 设备状态修改
export function changeDeviceStatus(deviceId, status) {
  const data = {
    deviceId,
    status
  }
  return request({
    url: '/energy/device/changeStatus',
    method: 'put',
    data: data
  })
}

// ==================== TDengine数据查询接口 ====================

// 查询设备最新数据
export function getDeviceLatestData(deviceCode) {
  return request({
    url: '/energy/device/data/latest/' + deviceCode,
    method: 'get'
  })
}

// 查询设备历史数据
export function getDeviceHistoryData(deviceCode, params) {
  return request({
    url: '/energy/device/data/history/' + deviceCode,
    method: 'get',
    params: params
  })
}

// 查询设备聚合数据
export function getDeviceAggregatedData(deviceCode, params) {
  return request({
    url: '/energy/device/data/aggregated/' + deviceCode,
    method: 'get',
    params: params
  })
}

// 查询所有设备最新数据
export function getAllDevicesLatestData() {
  return request({
    url: '/energy/device/data/latest/all',
    method: 'get'
  })
}

// 查询设备在线状态统计
export function getDeviceOnlineStatistics() {
  return request({
    url: '/energy/device/statistics/online',
    method: 'get'
  })
}

// 查询设备能耗排行榜
export function getDeviceEnergyRanking(params) {
  return request({
    url: '/energy/device/ranking/energy',
    method: 'get',
    params: params
  })
}

// 获取设备MQTT提交格式
export function getDeviceMqttFormat(deviceId) {
  return request({
    url: '/energy/device/mqtt-format/' + deviceId,
    method: 'get'
  })
}

// 获取设备状态统计
export function getDeviceStatusStatistics() {
  return request({
    url: '/energy/device/status/statistics',
    method: 'get'
  })
}